<template>
  <div class="auditTop">
    <div>
      <sui-grid>
        <sui-grid-column :width="3">
          <sui-menu fluid vertical tabular>
            <el-menu :default-active="defaultActive" @select="choseItem" active-text-color="#00b5ad">
              <el-menu-item index="1">待审核文件</el-menu-item>
              <el-menu-item index="2">上传文件</el-menu-item>
              <el-menu-item index="3">管理文件</el-menu-item>
              <el-menu-item v-if="identify == 'admin'" index="0">审核文件</el-menu-item>
            </el-menu>
          </sui-menu>
        </sui-grid-column>

        <sui-grid-column :width="13">
          <div class="ui container segment" v-if="this.$router.currentRoute.path == '/documentAudit'">
            <el-table :data="myFileLists" style="width: 100%" v-loading="isloading" fit>
              <el-table-column prop="realName" label="文件名" show-overflow-tooltip>
              </el-table-column>
              <el-table-column prop="uploadTime" :formatter="dateFormatter" label="上传时间">
              </el-table-column>
              <el-table-column prop="description" label="描述" show-overflow-tooltip>
              </el-table-column>
              <el-table-column prop="status" :formatter="statusFormatter" label="审核状态">
              </el-table-column>
              <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                  <el-popconfirm title="确认撤回此文件吗？" @confirm="backAudit(scope.row)" icon-color="red">
                    <el-button slot="reference" type="text">撤回</el-button>
                  </el-popconfirm>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <router-view />
        </sui-grid-column>
      </sui-grid>
    </div>
  </div>
</template>

<script>
export default {
  inject: ['reload'],
  data() {
    return {
      defaultActive: '1',        //默认激活菜单
      identify: JSON.parse(sessionStorage.getItem('userInfo')).role,            //用户身份
      isloading: false,                  //文件处理加载框
      myFileLists: null,           //待审批的文件
    };
  },
  methods: {
    backAudit(row) {
      const currentId = row.id;
      this.$https({
        method: 'delete',
        url: '/user/deleteFile',
        params: {
          fileId: currentId
        }
      }).then(resp => {
        if (resp.data.code == 200) {
          this.reload();
          this.$message.success("撤回成功！");
        }
        else
          this.$message.info(resp.data.data);
      })
    },
    getMyList() {
      this.$https.get('/user/myFileList').then((res) => {
        if (res.data.code == 200)
          this.myFileLists = res.data.data.filter(e => { return e.status == 1 || e.status == 3 });
        else
          this.$message.info(res.data.message);
      })
    },
    dateFormatter(row, column) {
      let datetime = row.uploadTime;
      return this.timestampToTime(datetime);
    },
    statusFormatter(row, column) {
      let sta = row.status;
      if (sta == 1)
        return '待审批';
      else if (sta == 3)
        return '驳回';
    },
    timestampToTime(time) {
      //时间戳转换
      // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var date = new Date(time);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      let d = date.getDate();
      let h = date.getHours();
      let m = date.getMinutes();
      let s = date.getSeconds();
      MM = MM < 10 ? "0" + MM : MM;
      d = d < 10 ? "0" + d : d;
      h = h < 10 ? "0" + h : h;
      m = m < 10 ? "0" + m : m;
      s = s < 10 ? "0" + s : s;
      // return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
      return y + "-" + MM + "-" + d;
    },
    choseItem(key) {
      if (key == '1')
        this.$router.push('/documentAudit');
      else if (key == '2')
        this.$router.push('/documentAudit/documentUpLoad');
      else if (key == '3')
        this.$router.push('/documentAudit/documentDelete');
      else if (key == '0')
        this.$router.push('/documentAudit/adminDocument')
    },
  },
  mounted() {
    if (this.$router.currentRoute.path == '/documentAudit/documentUpLoad')
      this.defaultActive = '2';
    else if (this.$router.currentRoute.path == '/documentAudit/documentDelete')
      this.defaultActive = '3';
    else if (this.$router.currentRoute.path == '/documentAudit/adminDocument')
      this.defaultActive = '0';
  },
  created() {
    this.getMyList();
  }
}
</script>

<style scoped>
.auditTop {
  padding-top: 5rem !important;
  padding-bottom: 5rem !important;
}
</style>